<?php
namespace App\Utils;

use Hyperf\HttpServer\Contract\RequestInterface;

class Helper
{

    /**
     * @param int $seconds 秒数
     * @return string 自然语言的时长
     */
    public static function countDown($seconds)
    {
        if ($seconds < 1) {
            return '';
        }
        $str = '';
        $day = floor($seconds / 86400);
        $restSec = $seconds % 86400;
        if ($day > 0) {
            $str .= '' . $day . '天';
        }
        if ($restSec <= 0) {
            return $str;
        }
        $h = floor($restSec / 3600);
        if ($day > 0 || $h > 0) {
            $str .= '' . $h . '小时';
        }
        $restSec = $restSec % 3600;
        if ($restSec <= 0) {
            return $str;
        }
        $i = floor($restSec / 60);
        if ($day > 0 || $h > 0 || $i > 0) {
            $str .= '' . $i . '分';
        }
        $restSec = $restSec % 60;
        if ($restSec <= 0) {
            return $str;
        }
        $str .= $restSec . '秒';
        return $str;
    }

    public static function resp($response, $code = 200, $msg = '', $data = null, $extData = null)
    {
        if ($data) {
            $data = self::coverBigintToString($data);
        }
        return $response->withHeader('Content-Type','application/json;Charset=utf8')
            ->json([
                'code' => $code,
                'msg' => $msg,
                'data' => $data,
                'ext_data' => $extData,
            ]);
    }

    public static function jsonBody($code = 200, $msg = '', $data = null, $extData = null)
    {
        return json_encode([
            'code' => $code,
            'msg' => $msg,
            'data' => $data,
            'ext_data' => $extData,
        ],320);
    }

    public static function coverBigintToString($data)
    {
        if ((is_int($data) || is_float($data)) && $data > 100000000000) {
            return '' . $data;
        }
        if (!is_array($data)) {
            return $data;
        }
        foreach ($data as &$item) {
            $item = self::coverBigintToString($item);
        }
        return $data;
    }

    public static function getRealIP($request=null)
    {
        if (!$request){
            $request = \Hyperf\Support\make(RequestInterface::class);
        }
        $realIP = $request->header('real_ip');
        if ($realIP){
            return $realIP;
        }
        $xIP = $request->header('x-real-ip');
        if ($xIP){
            return $xIP;
        }
        $ip = $request->header('remote_addr');
        return $ip ?: '';
    }

    public static function trimParams(array $params)
    {
        $res = [];
        foreach ($params as $k=>$v){
            if (is_array($v)){
                $res[$k] = self::trimParams($v);
            }elseif (is_string($v)){
                $res[$k] = trim($v);
            }else{
                $res[$k] = $v;
            }
        }
        return $res;
    }

    /**
     * 列表变成树形结构
     * @param $list
     * @param $pk
     * @param $pid
     * @param $child
     * @param $root
     * @return array
     */
    public static function generateTree($list, $pk = 'id', $pid = 'pid', $child = 'children', $root = 0)
    {
        $tree = array();
        $packData = array();
        foreach ($list as $data) {
            $packData[$data[$pk]] = $data;
        }
        foreach ($packData as $key => $val) {
            if ($val[$pid] == $root) {
                //代表跟节点, 重点一
                $tree[] = &$packData[$key];
            } else {
                //找到其父类,重点二
                $packData[$val[$pid]][$child][] = &$packData[$key];
            }
        }
        return $tree;
    }
}